//
//  ProgressView.m
//  Camera
//
//  Created by gml on 2018/10/29.
//  Copyright © 2018年 gml. All rights reserved.
//

#import "ProgressView.h"
#define KProgressBorderWidth 2.0f
#define KProgressPadding 1.0f
#define KProgressColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1]
@interface ProgressView ()
@property (nonatomic,strong) CAShapeLayer *progressLayer;

@end

@implementation ProgressView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
//        UIBezierPath *path = [UIBezierPath bezierPath];
//        [path moveToPoint:CGPointMake(0, 0)];
//        [path addLineToPoint:CGPointMake(self.bounds.size.width, 0)];
//        [path addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)];
//        [path addLineToPoint:CGPointMake(0, self.bounds.size.height)];
//        [path closePath];
        UIRectCorner corners = UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft | UIRectCornerTopLeft;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5)];

        // 边框
        CAShapeLayer *borderLayer = [CAShapeLayer layer];
//        borderLayer.frame = self.bounds;
        borderLayer.path = path.CGPath;
        borderLayer.fillColor = [UIColor whiteColor].CGColor;
        borderLayer.strokeColor = KProgressColor.CGColor;
        borderLayer.lineWidth = 1;
        borderLayer.lineJoin= kCALineJoinRound;
        borderLayer.lineCap= kCALineJoinRound;
        [self.layer addSublayer:borderLayer];
        self.backgroundColor = [UIColor yellowColor];
        
        // 边框
        CAShapeLayer *progressLayer = [CAShapeLayer layer];
        //        borderLayer.frame = self.bounds;
        progressLayer.path = path.CGPath;
        progressLayer.fillColor = [UIColor redColor].CGColor;
//        progressLayer.strokeColor = KProgressColor.CGColor;
        progressLayer.lineWidth = self.bounds.size.width-10;
        progressLayer.lineJoin= kCALineJoinRound;
        progressLayer.lineCap= kCALineJoinRound;
        [self.layer addSublayer:progressLayer];
        self.progressLayer = progressLayer;
        
    }
    return self;
}
- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    UIRectCorner corners = UIRectCornerTopRight | UIRectCornerBottomRight | UIRectCornerBottomLeft | UIRectCornerTopLeft;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width * progress, self.bounds.size.height) byRoundingCorners:corners cornerRadii:CGSizeMake(self.bounds.size.height*0.5, self.bounds.size.height*0.5)];
    self.progressLayer.path = path.CGPath;
}

@end
